package com.csust.automaticteach.manager.websocket;

import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket configuration for Yjs provider
 * webSocket 配置
 */
@EnableWebSocket
@RequiredArgsConstructor
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
    private final YjsWebSocketHandler yjsWebSocketHandler;
    @Resource
    private WsHandShakeInterceptor wsHandShakeInterceptor;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 匹配 /yjs/{room} 路径
        registry.addHandler(yjsWebSocketHandler, "/ws/{room}")
                .addInterceptors(wsHandShakeInterceptor)//拦截器
                .setAllowedOriginPatterns("*");   // 开发阶段允许跨域
    }
}